/**
 * 
 */
package nl.boektrust.btsoep.shared.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Embedded;
import javax.persistence.Transient;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Serialized;

/**
 * @author rao3
 *
 */
public class Contributor implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 150100176152324460L;
	private String nameOnBook;
	private ContributorRoleCode roleCode;
	// person is labeled javax.persistence.Transient so that it is not stored or retrieved; rather, the keys are
	// stored and retrieved within person. 
	@Transient private Person person;
	private Key<Person> personKey;

	/**
	 * @return the personKey
	 */
	public Key<Person> getPersonKey() {
		return personKey;
	}

	/**
	 * @param personKey the personKey to set
	 */
	public void setPersonKey(Key<Person> personKey) {
		this.personKey = personKey;
	}

	/**
	 * @return the person
	 */
	public Person getPerson() {
		return person;
	}

	/**
	 * @param person the person to set
	 */
	public void setPerson(Person person) {
		this.person = person;
	}

	/**
	 * @param nameOnBook
	 * @param roleCode
	 * @param person
	 */
	public Contributor(String nameOnBook,
			ContributorRoleCode roleCode, Person person) {
		this.nameOnBook = nameOnBook;
		this.roleCode = roleCode;
	//	if (person != null) this.person = person;
	}

	/**
	 * @param name
	 * @param roleCode
	 */
	public Contributor(String name, ContributorRoleCode roleCode) {
		this(name, roleCode, null);
		// TODO Auto-generated constructor stub
	}
	
	public Contributor() {
		this("", ContributorRoleCode.UNDEFINED, null);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @return the nameOnBook
	 */
	public String getNameOnBook() {
		return nameOnBook;
	}

	/**
	 * @param nameOnBook the nameOnBook to set
	 */
	public void setNameOnBook(String nameOnBook) {
		this.nameOnBook = nameOnBook;
	}

	/**
	 * @return the roleCode
	 */
	public ContributorRoleCode getRoleCode() {
		return roleCode;
	}

	/**
	 * @param roleCode the roleCode to set
	 */
	public void setRoleCode(ContributorRoleCode roleCode) {
		this.roleCode = roleCode;
	}

	
	public String toPrintString() {
		String string = "";
		if (this.nameOnBook != null) string = string + "   nameOnBook:  " + this.nameOnBook + "\n";
		string = string + "   role:        " + ContributorRoleCode.stringValue(this.roleCode) + "\n";
		if (this.person != null) {
			if (this.person.getName() != null) string = string + "   person Name: " + this.person.getName() + "\n";
			string = string + "   person ID:   " + this.person.getId() + "\n";
		}
		return string;
	}
	

	public ArrayList<String> populateParsedNameOnBook() {
		ArrayList<String>nameOnBookStrings = new ArrayList<String>();

		String str = this.getNameOnBook();
		//System.err.println("parsing name on book in populateParsedNameOnBook/Contributor: "+str);
			nameOnBookStrings = new ArrayList<String>();
		 String[] result = str.toLowerCase().split("[,\\.\\s]");
	     for (int x=0; x<result.length; x++) {
	    	 if (StopWords.isANameStopWord(result[x]) == false && result[x].length()>1) nameOnBookStrings.add(result[x]);
	     }
         /*System.err.print("Parsed as: ");
         for (String s : nameOnBookStrings) {
        	 System.err.print(s);
        	 System.err.print(", ");
         }
    	 System.err.println();
    	 */
         return nameOnBookStrings;
	}
	
}
